package net.xju3_4.controller;

import net.xju3_4.common.framework.simplemvc.RequestMapping;
import net.xju3_4.common.utils.DateUtils;
import net.xju3_4.common.utils.JSONUtil;
import net.xju3_4.common.utils.PageInfo;
import net.xju3_4.common.utils.Result;
import net.xju3_4.entity.Goods;
import net.xju3_4.entity.vo.GoodsVo;
import net.xju3_4.service.GoodsService;
import net.xju3_4.service.impl.GoodsServiceImpl;

import javax.naming.spi.DirObjectFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

@RequestMapping("/api/goods")
public class GoodsController {

    GoodsService goodsService = new GoodsServiceImpl();
    // 组员写代码了
    // 组员又写代码了
    // 组长写代码

    // 组员写代码
    // 组长写代码

    //添加
    @RequestMapping("/insert.do")
    public void insert(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        Goods goods = new Goods();
        goods.setName(request.getParameter("name"));
        goods.setPrice(Integer.parseInt(request.getParameter("price")));
        goods.setPubdate(DateUtils.parseStr2Date(request.getParameter("pubdate")));
        goods.setTypeId(Integer.parseInt(request.getParameter("typeId")));
        goods.setIntro(request.getParameter("intro"));
        goods.setPicture(request.getParameter("picture"));
        goods.setFlag(Integer.parseInt(request.getParameter("flag")));
        goods.setCreateTime(LocalDateTime.now());
        if (goodsService.insert(goods)) {
            JSONUtil.printByJSON(response, Result.success("添加成功"));
        } else {
            JSONUtil.printByJSON(response, Result.fail("添加失败"));
        }
    }
    // 删除
    @RequestMapping("/delete.do")
    public void delete(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        if (goodsService.delete(Integer.parseInt(id))) {
            JSONUtil.printByJSON(response, Result.success("删除成功"));
        } else {
            JSONUtil.printByJSON(response, Result.fail("删除失败"));
        }
    }
    // 修改
    @RequestMapping("/update.do")
    public void update(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        Goods goods = new Goods();
        goods.setId(Integer.parseInt(request.getParameter("id")));
        goods.setName(request.getParameter("name"));
        if (!request.getParameter("price").isEmpty()) {
            goods.setPrice(Integer.parseInt(request.getParameter("price")));
        }
        if (!request.getParameter("pubdate").isEmpty()) {
            goods.setPubdate(DateUtils.parseStr2Date(request.getParameter("pubdate")));
        }
        if (!request.getParameter("typeId").isEmpty()) {
            goods.setTypeId(Integer.parseInt(request.getParameter("typeId")));
        }
        goods.setIntro(request.getParameter("intro"));
        goods.setPicture(request.getParameter("picture"));
        if (!request.getParameter("flag").isEmpty()) {
            goods.setFlag(Integer.parseInt(request.getParameter("flag")));
        }
        String deletedStr = request.getParameter("deleted");
        if (deletedStr != null && !deletedStr.isEmpty()) {
            goods.setDeleted(Integer.parseInt(deletedStr));
        }
        if (goodsService.update(goods)) {
            JSONUtil.printByJSON(response, Result.success("修改成功"));
        } else {
            JSONUtil.printByJSON(response,Result.fail("修改失败"));
        }
    }
    // 分页条件查询
    @RequestMapping("/pageList.do")
    public void pageList(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        // 分页参数
        String pageNoStr = request.getParameter("pageNo");
        String pageSizeStr = request.getParameter("pageSize");
        Integer pageNo = pageNoStr == null || pageSizeStr.isEmpty() ? 1 : Integer.parseInt(pageNoStr);
        Integer pageSize =pageNoStr==null || pageSizeStr.isEmpty() ? 10 : Integer.parseInt(pageSizeStr);
        // 条件
        Goods goods = new Goods();
        goods.setName(request.getParameter("name"));
        if (!request.getParameter("pubdate").isEmpty()) {
            goods.setPubdate(DateUtils.parseStr2Date(request.getParameter("pubdate")));
        }
        if (!request.getParameter("typeId").isEmpty()) {
            goods.setTypeId(Integer.parseInt(request.getParameter("typeId")));
        }
        if (!request.getParameter("flag").isEmpty()) {
            goods.setFlag(Integer.parseInt(request.getParameter("flag")));
        }
        PageInfo<GoodsVo> pageInfo =  goodsService.pageList(pageNo, pageSize, goods);
        JSONUtil.printByJSON(response,Result.success(pageInfo));
    }
}
